package com.yipao.admin.web.goods;

import com.yipao.admin.annotation.RequiresPermissionsDesc;
import com.yipao.core.constant.GlobalConstant;
import com.yipao.core.util.ResponseUtil;
import com.yipao.core.validator.Order;
import com.yipao.core.validator.Sort;
import com.yipao.db.domain.goods.GoodsMaterial;
import com.yipao.db.domain.goods.vo.GoodsMaterialReq;
import com.yipao.db.domain.goods.vo.GoodsMaterialVo;
import com.yipao.db.service.goods.GoodsFormulaService;
import com.yipao.db.service.goods.GoodsMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品原料
 *
 * @author tianhao
 * @date 2020/4/29 18:27
 **/
@RestController
@RequestMapping("/admin/goods/material")
@Validated
public class AdminGoodsMaterialController {
    @Resource
    private GoodsMaterialService goodsMaterialService;
    @Resource
    private GoodsFormulaService goodsFormulaService;

    /**
     * 删除商品原料
     *
     * @param
     * @return
     */
    @RequiresPermissions("admin:goods:material:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "原料管理"}, button = "删除")
    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        if (StringUtils.isEmpty(id)) {
            return ResponseUtil.badArgument();
        }
        // 原料被配方使用,不能删除
        // 根据原料id查询商品配方数量
        int count = goodsFormulaService.queryCountFormulaByMaterialId(id);
        if (count > 0) {
            return ResponseUtil.fail(ResponseUtil.STATUS_CODE_UN_AUTH, "删除失败,请先删除对应的配方");
        }
        goodsMaterialService.delete(id);
        return ResponseUtil.ok();
    }

    /**
     * 新增商品原料
     *
     * @param goodsMaterial
     * @return
     */
    @RequiresPermissions("admin:goods::material:insert")
    @RequiresPermissionsDesc(menu = {"商品管理", "原料管理"}, button = "新增")
    @PostMapping("/insert")
    public Object insert(@RequestBody GoodsMaterial goodsMaterial) {
        goodsMaterialService.insert(goodsMaterial);
        return ResponseUtil.ok(goodsMaterial);
    }

    /**
     * 修改商品原料
     *
     * @param goodsMaterial
     * @return
     */
    @RequiresPermissions("admin:goods::material:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "原料管理"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsMaterial goodsMaterial) {
        if (StringUtils.isEmpty(goodsMaterial.getId())) {
            return ResponseUtil.badArgument();
        }
        goodsMaterialService.update(goodsMaterial);
        return ResponseUtil.ok(goodsMaterial);
    }

    /**
     * 原料管理列表
     *
     * @param page  页码
     * @param limit 每页显示的记录数
     * @param sort  排序字段,默认创建时间
     * @param order 排序规则,默认降序,即先创建的,排在前面
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:goods::material:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "原料管理"}, button = "查询")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return ResponseUtil.okList(goodsMaterialService.selectByExample(
                new GoodsMaterialReq(page, limit, sort, order, GlobalConstant.DeleteStatus.NOT_DELETED)));
    }

    /**
     * 商品原料详情
     *
     * @param id
     * @return
     */

    @GetMapping("/detail")
    @RequiresPermissions("admin:goods:material:detail")
    @RequiresPermissionsDesc(menu = {"商品管理", "原料管理"}, button = "详情")
    public Object detail(@NotNull Integer id) {
        return goodsMaterialService.queryById(id);
    }

    /**
     * 查询全部商品原料
     *
     * @return
     */
    @GetMapping("/all")
    @RequiresPermissions("admin:goods:material:all")
    @RequiresPermissionsDesc(menu = {"商品管理", "原料管理"}, button = "查询全部")
    public Object queryAll() {
        List<GoodsMaterialVo> voList = new ArrayList<>();

        List<GoodsMaterial> goodsMaterials = goodsMaterialService.queryAll();
        GoodsMaterialVo vo;
        for (GoodsMaterial goodsMaterial : goodsMaterials) {
            vo = new GoodsMaterialVo();
            vo.setId(goodsMaterial.getId());
            vo.setText(goodsMaterial.getName());
            vo.setValue(goodsMaterial.getId() + "");
            voList.add(vo);
        }
        return ResponseUtil.okList(voList);
    }
}
